﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.Forms;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.ApplicationContainer;
using jSLCore.Client.Presentation.BaseControl.Selection;
using jSLCore.Client.Presentation.jBinding;
using jSLCore.Client.Presentation.jBinding.BaseConverters;
using jSLCore.Client.Presentation.Util;

namespace jSLApp.Client.Controls
{
    public partial class CommentGrid : jSLGrid
    {
        public CommentGrid()
        {
            InitializeComponent();

            Style = (Style)Resources["selGrid"];
        }

        private bool _isRatingVisible;
        public bool IsRatingVisible
        {
            get { return _isRatingVisible; }
            set { _isRatingVisible = value; }
        }

        private void gotoUser_Clicked(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            var login = (string) element.Tag;

            MainContentContainer.Current.GotoUserAbout(login);
        }

        private void NeedIt(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            if(!IsRatingVisible)element.Visibility = Visibility.Collapsed;
        }

        private void reply_Clicked(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            var comment = (Comment)element.DataContext;

            InvokeReply(
                new QuoteEventArgs(
                    string.Format("{0} {1}  "
                    ,comment.CreatorNameText,new jFormatDateTimeConverter()
                    .Convert(comment.CreationDate,typeof(string),null,CultureInfo.CurrentCulture)),comment.Text));
        }

        public event EventHandler<QuoteEventArgs> Reply;

        public void InvokeReply(QuoteEventArgs e)
        {
            EventHandler<QuoteEventArgs> handler = Reply;
            if (handler != null) handler(this, e);
        }

        public event EventHandler<GenericEventArgs<object>>  DeleteClick;

        public void InvokeDeleteClicked(object param)
        {
            EventHandler<GenericEventArgs<object>> handler = DeleteClick;
            if (handler != null) handler(this, new GenericEventArgs<object>(param));
        }

        private void comment_delete(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            InvokeDeleteClicked(element.DataContext);

        }
    }


    public class QuoteEventArgs:EventArgs
    {
        public readonly string WhoAndWhenQuote;
        public readonly string QuoteText;

        public QuoteEventArgs(string whoAndWhenQuote, string quoteText)
        {
            WhoAndWhenQuote = whoAndWhenQuote;
            QuoteText = quoteText;
        }
    }
}
